function isInsight(el){
    // 通过getBoundingClientRect()方法获得元素位置相对各边的距离
    const bound=el.getBoundingClientRect();
    // 窗口高度
    const clientHeight=window.innerHeight;
    // 元素是否进入可视区域的一半
    return bound.top <= clientHeight + 100
}

// 检查元素是否出现在可视区域
let index=0;
function checkImgs(){
    const imgs=document.querySelectorAll('.img');
    for(let i=index,len=imgs.length;i<len;i++){
        if(isInsight(imgs[i])){
            loadImg(imgs[i]);   //加载图片 将data-src的内容替换 src内容
            index = i;
        }
    }
}

function loadImg(el){
    const source=el.dataset.src;
    el.src=source;
}


// 函数节流

function throttle(fn, mustRun = 1000) {
    
    const timer = null;
    let previous = null;
    return function() {
      const now = new Date();
      const context = this;
      const args = arguments;
      if (!previous) {
        
        previous = now;
      }
      const remaining = now - previous;
      if (mustRun && remaining >= mustRun) {
        fn.apply(context, args);
        previous = now;
      }
    }
  }